CREATE PROCEDURE #PK_UpdateStaticPassword
AS 

DECLARE cursorUser CURSOR FOR SELECT TokenSn,TokenSnBak FROM KOperators WHERE TokenSn='static' FOR UPDATE

OPEN cursorUser

DECLARE @TokenSn VARCHAR(50)
DECLARE @Password VARCHAR(50)
DECLARE @PasswordID int
FETCH NEXT FROM cursorUser INTO @TokenSn,@Password
WHILE (@@fetch_status=0)
BEGIN
	INSERT INTO KPassword(Type) VALUES(1)
	SET @PasswordID = @@IDENTITY
	INSERT INTO KPasswordStatical(ID,Password) VALUES(@PasswordID,@Password)
	UPDATE KOperators SET TokenSn=@PasswordID,TokenSnBak=NULL WHERE CURRENT OF cursorUser
	FETCH NEXT FROM cursorUser INTO @TokenSn,@Password
END

CLOSE cursorUser

DEALLOCATE cursorUser

GO

EXEC #PK_UpdateStaticPassword

GO